package com.CMUBiS.Depreciated;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.CMUBiS.Data.GlobalVars;
import com.CMUBiS.util.Bid;
import com.CMUBiS.util.Item;

@WebServlet(name="MyAccountServletold", urlPatterns="/myaccountold")
public class MyAccountServlet extends HttpServlet{
	private static final long serialVersionUID = 6475341003023438743L;
	private static final String EMPTY = "";
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		StringBuffer errorString = new StringBuffer();
		Map<String, String[]> paramMap = request.getParameterMap();
		
		if(!paramMap.isEmpty()){
			int userID=-1;
			String authToken=null;
			
			/*
			 * Check the userID is present and correct
			 */
			if(!paramMap.containsKey("userID") || request.getParameter("userID").equals(EMPTY)){
					errorString.append("You must provide a userID. <br />");
			}
			else{
				// make sure the userID is a number
				try{
					userID = Integer.parseInt(request.getParameter("userID"));
					
					if(userID <0)
						errorString.append("The userID must be greater than zero <br />");
				}
				catch(Exception e){
					errorString.append("The userID must be a number. <br />");
				}
			}
			
			/*
			 * Check the auth token is passed
			 */
			if(!paramMap.containsKey("authToken") || request.getParameter("authToken").equals(EMPTY)){
				errorString.append("You must provide an auth token. <br />");
			}
			else
				authToken = request.getParameter("authToken");
			
			// If the username and auth token are present we need to check they are correct before we read the user's info
			if(errorString.toString().equals(EMPTY)){
				if(GlobalVars.db.checkAuthToken(userID, authToken)){
					// if everything is good, we can make the page
					this.makePage(request, response, errorString.toString(), userID, authToken);
				}
				else
					errorString.append("Your userID and authToken don't match. (userID: "+ userID + ", authToken: " + authToken + "<br />");
			}
		}
		else{
			errorString.append("You must be logged in to view your account. <br />");
		}
		
		// If we get to here then there must be an error
		this.makePage(request, response, errorString.toString(), -1, null);
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @param errorString
	 * @throws ServletException
	 * @throws IOException
	 */
	public void makePage(HttpServletRequest request, HttpServletResponse response, String errorString, int userID, String authToken)  throws ServletException, IOException {
		// We are using PrintWriter to be friendly to the international community. PrintWrite auto-converts coding
		PrintWriter out = response.getWriter();
		
		// Include header
		out.println("<HTML>");
		out.println("<HEAD><TITLE>Register User</TITLE></HEAD>");
		out.println("<BODY>");
		out.println(errorString);
		
		// Do HTML4 or 5 depending on the global variables
		if(GlobalVars.isHTML4)
			createHTML4(request, response, out, userID, authToken);
		else
			createHTML5(request, response, out, userID, authToken);
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @param out
	 * @return
	 */
	public HttpServletResponse createHTML4(HttpServletRequest request, HttpServletResponse response, PrintWriter out, int userID, String authToken){
		try{
			// Set the return type
			response.setContentType("text/html");
			
			if(userID>0){
				// Get the URL parameters if any were passed
				Map<String, String[]> paramMap = request.getParameterMap();
				
				//TODO: first welcome links...
				out.println("Welcome " + GlobalVars.db.getFirstName(userID)+ "! (userID: " + userID + ") <br />");
				
				/*
				 * Print out the users options from the account
				 */
				out.println("<BR /><a href=\"./searchcategory?userID="+userID+"&authToken="+authToken+"\">search</a><BR />");
				
				out.println("<BR /><a href=\"./browsecategory?userID="+userID+"&authToken="+authToken+"\">browse</a><BR />");
				
				out.println("<BR /><a href=\"./updateuserdetails?userID="+userID+"&authToken="+authToken+"\">Update details</a><BR />");
				
				/*
				 * Print out the users current and winning bids
				 */
				ArrayList<Bid> currentBids = GlobalVars.db.getCurrentBids(userID);
				
				out.println("<BR />Current Bids:");
				if(currentBids==null || currentBids.isEmpty()){
					out.println("<div id=\"entry\">");
					out.println("None");
					out.println("</div>");
				}
				else{			
					for(Bid currentBid:currentBids){
						out.println("<div id=\"entry\">");
						out.println("<a href=\"viewitem?userID=" + userID + "&authToken="+authToken + "&itemID=" + currentBid.getItem().getID() + "\">");
						out.println(currentBid.getItem().getName());
						out.println("</a>");
						out.println(currentBid.getMaxBid());
						out.println(currentBid.getBidDate());
						out.println(currentBid.getItem().getEndDate());
						out.println("</div>");
					}
				}
				
				/*
				 * Print out the old bids
				 */
				
				ArrayList<Bid> oldBids = GlobalVars.db.getOldBids(userID);
				
				out.println("<BR />Auctions that have ended:");
				if(oldBids==null || oldBids.isEmpty()){
					out.println("<div id=\"entry\">");
					out.println("None");
					out.println("</div>");
				}
				
				for(Bid currentBid:oldBids){
					out.println("<div id=\"entry\">");
					out.println("<a href=\"viewitem?userID=" + userID + "&authToken="+authToken + "&itemID=" + currentBid.getItem().getID() + "&old=1\">");
					out.println(currentBid.getItem().getName());
					out.println("</a>");
					out.println(currentBid.getMaxBid());
					out.println(currentBid.getBidDate());
					out.println(currentBid.getItem().getEndDate());
					out.println("</div>");
				}
				
				/*
				 * Print out items we are selling
				 */
				ArrayList<Item> sellingItems = GlobalVars.db.getCurrentSellingItems(userID);
				
				out.println("<BR />Items you are selling:");
				if(sellingItems==null || sellingItems.isEmpty()){
					out.println("<div id=\"entry\">");
					out.println("None");
					out.println("</div>");
				}
				
				for(Item item:sellingItems){
					out.println("<div id=\"entry\">");
					out.println(item.getName());
					out.println(item.getEndDate());
					out.println("</div>");
				}
				
				/*
				 * Print out items we have previously sold selling
				 */
				ArrayList<Item> oldSellingItems = GlobalVars.db.getOldSellingItems(userID);
				
				out.println("<BR />Items you have sold:");
				if(oldSellingItems==null || oldSellingItems.isEmpty()){
					out.println("<div id=\"entry\">");
					out.println("None");
					out.println("</div>");
				}
				
				for(Item item:oldSellingItems){
					out.println("<div id=\"entry\">");
					out.println(item.getName());
					out.println(item.getEndDate());
					out.println("</div>");
				}
				
				
				out.println("");
				/*
				out.println("<form name=\"login\" action=\"login\" method=\"POST\">");
				out.println("<div id=\"center\">");	
				
				// Make the username box
				out.println("<div id=\"floatright\">Username: </div>");
				out.println("<div id=\"floatcenter\"> <input type=\"text\" name=\"username\" size=\"30\" value=\""+ (paramMap.containsKey("username") ?  request.getParameter("username") : "") + "\"/> </div>");
				
				// Make the password box
				out.println("<div id=\"floatright\">Password: </div>");
				out.println("<div id=\"floatcenter\"> <input type=\"text\" name=\"password\" size=\"30\" value=\""+ (paramMap.containsKey("password") ?  request.getParameter("password") : "") + "\"/> </div>");
				
				out.println("<div id=\"floatright\"><input type=\"submit\" value=\"Login\"></div>");
				
				out.println("</div>");
				out.println("</form>");
				*/
			}
			
			
			// Include footer
			out.println("</BODY></HTML>");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
	
	public HttpServletResponse createHTML5(HttpServletRequest request, HttpServletResponse response, PrintWriter out, int userID, String authToken){
		try{
			
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
}
